/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import config.Models.Conf_BoxEgg;
import db.Redis_Rd;
import db.Redis_W;
import entities.Box_Egg;
import entities.Box_Egg.BoxEggDB;
import entities.Cote;
import entities.User;
import java.util.HashMap;
import java.util.Map;
import libCore.Util;
import share.ShareMacros;
import org.json.simple.JSONValue;
import share.KeysDefinition;

/**
 *
 * @author LinhTA
 */
public class U_BoxEggModel implements BaseModel {

    public Map<String, Box_Egg> boxs;
    public String uId;

    public U_BoxEggModel(String uid) 
    {
        uId = uid;
        boxs = new HashMap<String, Box_Egg>();
    }

    @Override
    public void setDB() 
    {
    }

    @Override
    public void setData(Map<String, String> obj) 
    {
    }

    @Override
    public void getDB(String uid) 
    {
    }

    public Map<String, Object> toAMF() 
    {
        Map<String, Object> data = new HashMap<String, Object>();

        for (Map.Entry<String, Box_Egg> entry : boxs.entrySet()) {
            String string = entry.getKey();
            Box_Egg object = entry.getValue();

            data.put(string, object.toAMF());
        }

        return data;
    }

    public static Box_Egg isNewUser(String uid, String keyBoxEgg, long time) 
    {
        Box_Egg box = new Box_Egg();

        box = boxDefault(time);
        
        String keyEggs  =   KeysDefinition.randomKeySTR();
        
        BoxEggDB boxEggDB   =   new Box_Egg.BoxEggDB();
        boxEggDB.level      =   String.valueOf( box.level );
        boxEggDB.eggs       =   keyEggs;
              
        if (Redis_W.getInstance().set( keyBoxEgg, Util.obj2String(boxEggDB)) != -1) {
            //boxs.put( keyBoxEgg, box );
        }

//        if (Redis_W.getInstance().sadd( boxEggDB.eggs, "") != -1) {
//            //boxs.put( keyBoxEgg, box );
//        }
        
        return box;
    }

    public static Box_Egg boxDefault(long time) 
    {
        Box_Egg box = new Box_Egg();
        box.level = 1;

        return box;
    }

    public Box_Egg getDBData(String key) 
    {
        Box_Egg box = new Box_Egg();

        String strBox = Redis_Rd.getInstance().get(key);
        Map<String, Object> data = new HashMap<String, Object>();
        data = (HashMap<String, Object>) JSONValue.parse(strBox);

        box.level = Integer.parseInt(data.get(ShareMacros.LEVEL).toString());
        box.eggs = U_EggModel.getDBEggs(data.get(ShareMacros.EGGS).toString());

        return box;
    }

    public static Box_Egg parseEggs(Map<String, Object> obj) 
    {
        Box_Egg box = new Box_Egg();

        box.level = Integer.parseInt(obj.get(ShareMacros.LEVEL).toString());
        box.eggs = U_EggModel.parseEggs(Util.obj2Map(obj.get(ShareMacros.EGGS)));

        return box;
    }

    public BoxEggDB getBoxEggDB( String keyBoxEgg ) 
    {
        
        String boxEggStr = Redis_Rd.getInstance().get(keyBoxEgg);
        Map<String, String> data = new HashMap<String, String>();
        data = (HashMap<String, String>) JSONValue.parse(boxEggStr);
        
        BoxEggDB boxEggDB = new BoxEggDB();
        boxEggDB.setData(data);
        
        return boxEggDB;
    }
    
    //==================== Hieu =============================
    /*
     * Upgrade box egg level of cote by one
     * @uid : user id
     * @coteId : current cote want to upgrade
     */
    public Boolean upgradeBoxEggByGold( String coteId ) 
    {
        
        Boolean result  =   false;
        
        UserModel uModel    =   new UserModel( uId );
        if( uModel.isValidCote( coteId ) ) {
            
            String coteStr = Redis_Rd.getInstance().get( coteId );
            Map<String, String> data = (HashMap<String, String>) JSONValue.parse(coteStr);
            Cote.CoteDB coteDB = new Cote.CoteDB();
            coteDB.setData(data);
            
            // It's current boxegg belong to cote
            Box_Egg boxegg  =   getDBData( coteDB.boxegg );
            
            // Get current user information
            User user       =   uModel.getDBUser();
            
            // Get next config box egg
            Map<String, Conf_BoxEgg> mconfBoxEgg    =   config.ConfigModels.conf_boxEggs;
            Conf_BoxEgg confBoxEgg                  =   mconfBoxEgg.get( String.valueOf( boxegg.level + 1 ) );
            
            if( confBoxEgg.require.gold <= user.gold && confBoxEgg.require.level <= user.level ) {
                
                // Increase box egg level
                boxegg.level        +=  1;
                String boxEggStr    =   Util.obj2String( boxegg );
                Redis_W.getInstance().set( coteDB.boxegg, boxEggStr );
                
                // Update user gold
                return uModel.updateUserGold( user.gold - confBoxEgg.require.gold );
            }
        }
        return result;
    }
        /*
     * Upgrade box egg level of cote by one
     * @uid : user id
     * @coteId : current cote want to upgrade
     */
    public Boolean upgradeBoxEggByCoin( String coteId ) 
    {
        Boolean result  =   false;
        
        UserModel uModel    =   new UserModel( uId );
        if( uModel.isValidCote( coteId ) ) 
        {
            String coteStr = Redis_Rd.getInstance().get( coteId );
            Map<String, String> data = (HashMap<String, String>) JSONValue.parse(coteStr);
            Cote.CoteDB coteDB = new Cote.CoteDB();
            coteDB.setData(data);
            
            // It's current boxegg belong to cote
            Box_Egg boxegg  =   getDBData( coteDB.boxegg );
            
            // Get current user information
            User user       =   uModel.getDBUser();
            
            // Get next config box egg
            Map<String, Conf_BoxEgg> mconfBoxEgg    =   config.ConfigModels.conf_boxEggs;
            Conf_BoxEgg confBoxEgg                  =   mconfBoxEgg.get( String.valueOf( boxegg.level + 1 ) );
            
            if( confBoxEgg.require.coin <= user.coin && confBoxEgg.require.level <= user.level ) {
                // Increase box egg level
                boxegg.level        +=  1;
                Redis_W.getInstance().set( coteDB.boxegg, Util.obj2String( boxegg ) );
                
                // Update user gold
                return uModel.updateUserCoin( user.coin - confBoxEgg.require.coin );
            }
        }
        return result;
    }
}
